//
//  Queue.swift
//  DemoUI
//
//  Created by 白瑾浩 on 2022/4/7.
//

import Foundation
/*队列,特点:FIFO（ first-in first-out order：先进先出）*/
public struct Queue<T> {
    fileprivate var list = Array<T>()
    private var isEmpty:Bool {
        return list.count == 0
    }

    //入列
    public mutating func enQueue(_ element:T) {
        list.append(element)
    }

    //出列
    public mutating func deQueue() ->T?{
        if list.isEmpty == false {
            let first = list.first
            list.remove(at: 0)
            return first
        } else {
            return nil
        }
    }

    //peek 查看队列的第一个元素
    public func peek() -> T? {
        return list.first
    }
}

//用于打印
extension Queue: CustomStringConvertible {
    public var description: String {
        let topDivider = "---Queue---\n"
        let bottomDivider = "-----------\n"
        var content = ""
        for item in list {
            content += "\(item)\n"
        }
        return topDivider + content + bottomDivider
    }
}
